package com.boot.service;

import com.boot.entity.User;
import com.boot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public List<User> list(Integer current,Integer pageSize){
        Integer start = (current - 1) * pageSize;
        return userMapper.list(start,pageSize);
    }

    public String doLogin(String userName, String password) {
        List<User> users = userMapper.getByUserNameAndPassword(userName,password);

        return users!=null && users.size() == 1 ? "success":"error";

    }

    public void delById(Long id) {
        userMapper.delById(id);
    }

    public void deleteAll(List<Long> ids) {
        userMapper.deleteAll(ids);
    }

    public void saveOrUpdate(User user) {
        if(user.getId()!=null){
            userMapper.updateById(user);
        }else{
            userMapper.save(user);
        }
    }

    public User getById(Long id) {
        return userMapper.getById(id);
    }

    public Integer pages(Integer current, Integer pageSize) {
        int count = userMapper.count();
        return count % pageSize == 0 ? count / pageSize : count / pageSize + 1 ;
    }

    public List<User> getByUserName(String userName) {
        List<User> users =  userMapper.getByUserName(userName);
        return users;
    }
}
